package com.all.spi.controller;

import com.all.spi.service.Phone;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @Author LY
 * @create 2024/3/8 10:33
 */
@RestController
public class SpiController {

    @GetMapping("/getPhone")
    public void getPhone(){
        System.out.println("测试java原生spi");
        // 执行Java SPI的规范
        ServiceLoader<Phone> phoneLoader = ServiceLoader.load(Phone.class);
        // 获取迭代器
        Iterator<Phone> it = phoneLoader.iterator();
        // 迭代遍历，输出集合中的所有元素
        while (it.hasNext()) {
            Phone phone = it.next();
            phone.getName();
        }
    }
}
